<?php

namespace App\Admin\Attributes;

use Illuminate\Support\Str;

trait HasModelUser
{
	public function parentNameText()
	{
		if ($parent = $this->parent) {
			$id = $parent->id;
			$name = Str::limit($parent->name, 15);
			return "上级：{$id} - {$name}";
		}
		return '上级：无';
	}
	
	public function gradeText()
	{
		return config("site.user.grades.{$this->grade}");
	}
	
	public function nameText($isRich = false)
	{
		$name = Str::limit($this->name, 15);
		
		if ($isRich) {
			$parentName = $this->parentNameText();
			$html  = "<div><small>{$parentName}</small></div>";
			$html .= "<div><b>{$this->id} - {$name}</b></div>";
			return $html;
		}
		
		return $name;
	}

	public function namePhone($isRich = false)
	{
		$name = Str::limit($this->name, 15);
		if(!$name){

			$name = Str::limit($this->phone, 15);
		}
		
		
		if ($isRich) {
			$parentName = $this->parentNameText();
			$html  = "<div><small>{$parentName}</small></div>";
			$html .= "<div><b>{$this->id} - {$name}</b></div>";
			return $html;
		}
		
		return $name;
	}
	
	public function phoneBoundText($isRich = false)
	{
		if ($isRich) {
			return $this->phone ? _label('已绑定', 'success') : _label('未绑定', 'default');
		}
		return $this->phone ? '已绑定' : '未绑定';
	}
	
	public function wechatBoundText($isRich = false)
	{
		if ($isRich) {
			return $this->wx_openid ? _label('已绑定', 'success') : _label('未绑定', 'default');
		}
		return $this->wx_openid ? '已绑定' : '未绑定';
	}
	
	public function weminiBoundText($isRich = false)
	{
		if ($isRich) {
			return $this->wm_openid ? _label('已绑定', 'success') : _label('未绑定', 'default');
		}
		return $this->wm_openid ? '已绑定' : '未绑定';
	}
	
	public function alipayBoundText($isRich = false)
	{
		if ($isRich) {
			return $this->alipay_user_id ? _label('已绑定', 'success') : _label('未绑定', 'default');
		}
		return $this->alipay_user_id ? '已绑定' : '未绑定';
	}
	
	public function lastEnteredText()
	{
		return $this->last_entered_at ? (string)$this->last_entered_at : '暂未访问';
	}
	
	public function lockedText($isRich = false)
	{
		if ($isRich) {
			return $this->is_locked ? _label('锁定', 'danger') : _label('正常', 'success');
		}
		return $this->is_locked ? '锁定' : '正常';
	}

	public function bankBoundText($isRich = false)
	{
		if ($isRich) {
			return $this->cert_number ? _label('已绑定', 'success') : _label('未绑定', 'default');
		}
		return $this->cert_number ? '已绑定' : '未绑定';
	}
}
